<title>监听网络请求</title>
<meta charset="UTF-8" />
<iframe src="index.html"  style="position: absolute;right:0;top:1%;min-height: 300px;border-width: 0px">
</iframe>
<h1>4.耳听八方--监听网络请求</h1>
<xmp>
    监控指定的请求，读取请求返回的结果
    语法 ListenUrl:请求url需要包含的字符串>返回结果需要包含的字符串
    如$op['prejs'][]="ListenUrl:track/api/Track/GetStatus>trackDetails";
    可以和ajax多种方式混用
</xmp>
<h1>
    示例
</h1>
<h2>Fedex</h2>
<xmp  style="background:#f0f0f0;white-space: pre;text-align: left" >
    $url = "https://www.fedex.com/fedextrack/?trknbr=".$finalNo."&".time();
    $op=[
    "timeout"=>120 , //超时时间
    // "selector"=>".history",//等待css选择器
    "selector"=>"html",
    "debug"=>0,
    //   "Headless"=>0,
    ];
    $op['js']=[];
    //监听包含shipments接口，且返回结果包含output
    $op['prejs'][]="ListenUrl:shipments>output";
    $op['js'][]="Sleep:5";
    $op['js'][]=<<<JS
        setInterval(
        function() {
        if(window.document.body.innerHTML.indexOf("Order Not Found")>=0){
    window.document.body.innerHTML ='<div id="js_result" class="order_input"  >Order Not Found</div>'
    }

    },4000
    )
    JS;

    $op['js'][]=<<<JS
    //  window.location.reload();
    JS;

    $result_str= HttpClient::RpcChromedpGet($url,[],$op,$ip,$port);

</xmp>

<h2>UPS,可覆盖原生XMLHttpRequest对象，提升成功率</h2>
<xmp  style="background:#f0f0f0;white-space: pre;text-align: left" >
        $func="";
        $url = 'https://www.ups.com/track?loc=en_US&requester=QUIC&'.time().'&tracknum='.$finalNo;
        //$url ="https://myip.top?".time();
        $header=[];
        $op = [
            "timeout"=>90 , //超时时间
            "selector"=>"html",//等待css选择器
            "debug"=>0,
            "input"=>'#stApp_trackAgain_trackingNumEntry',
            "button"=>'#stApp_trackAgain_getTrack',
            'wno'=>$finalNo
        ];
        $op['prejs'][]="document.getElementById('js_result').outerHTML='';";
        $op['prejs'][]="ListenUrl:track/api/Track/GetStatus>trackDetails";
        $op['js'][]="WaitVisible:".$op['button'];
        $op['js'][]="Sleep:2";

        $button = $op["button"];
        $input=  $op["input"];
        $op['js'][]=<<<JS
        if(document.querySelector('$input')&&document.querySelector('$input').value==''){.
            document.querySelector('$input').value='$finalNo';
            document.querySelector('$button').click();
         }
JS;
          $op['js'][]=<<<JS
        var originalXMLHttpRequest = window.XMLHttpRequest;
// 覆盖原生XMLHttpRequest对象,可选
window.XMLHttpRequest = function() {
  var xhr = new originalXMLHttpRequest();
  // 监听原生XMLHttpRequest对象的onreadystatechange事件
  xhr.addEventListener('readystatechange', function() {
    if (xhr.readyState === 4) { // 请求已完成
	 // console.log(xhr.responseURL);
	  //console.log('Response:', xhr.responseText);
	  if (xhr.responseURL.indexOf("track/api/Track/GetStatus")>=0){
	        console.log("自定义请求成功");
	     window.document.body.innerHTML +='<script id="js_result" class="order_input" type="application/json"  >'+xhr.responseText+'</script>'
	  }

    }
  });
  return xhr;
};

JS;

    $op['js'][]=<<<JS

//xhr.send();
    setInterval(
            function() {
              if(window.document.body.innerHTML.indexOf("The tracking number you entered is not valid")>=0){
                    window.document.body.innerHTML +='<div id="js_result" class="order_input"  >The tracking number you entered is not valid</div>'
                }

            },2000
        )

JS;
    //休眠10s
      $op['js'][]="Sleep:10";
        $op['js'][]=<<<JS
        if (!document.getElementById("js_result")){
          window.location.reload();
        }
JS;

        $ua_str="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36";
        $content =  HttpClient::RpcChromedpGet($url,$header,$op,$ip,$port,$ua_str,$func);
</xmp>